package com.wangsai.framework.supersecurity.core.context.chain;

import com.wangsai.framework.supersecurity.core.UserRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.Wang
 * @program: super-security
 * @date 2021-08-14 16:42
 */
public class UserContextFilterChain implements UserContextFilter {

    private int pos = 0;

    private List<UserContextFilter> userContextFilterList;

    public void addFilter(UserContextFilter userContextFilter) {
        if (userContextFilterList == null) {
            userContextFilterList = new ArrayList<>();
        }
        userContextFilterList.add(userContextFilter);
    }

    @Override
    public void doFilter(UserRequest userRequest, UserContextFilterChain filterChain) {
        if (userContextFilterList == null) {
            return;
        }
        if (pos == userContextFilterList.size()) {
            return;
        }
        // 所有过滤器执行完毕
        userContextFilterList.get(pos++).doFilter(userRequest, filterChain);
    }
}
